<template>
  <div class="register">
      <el-form 
        :model="ruleForm" 
        status-icon 
        :rules="rules" 
        ref="ruleForm" 
        class="register-box">

        <div class="title">用户注册</div>

        <el-form-item  prop="phone">
          <el-input type="text" v-model="ruleForm.phone" autocomplete="off" placeholder="用户名"></el-input>
        </el-form-item>

        <el-form-item prop="name">
          <el-input type="text" v-model="ruleForm.name" autocomplete="off" placeholder="姓名"></el-input>
        </el-form-item>


        <el-form-item prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="密码"></el-input>
        </el-form-item>

        <el-form-item prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="确认密码"></el-input>
        </el-form-item>

        <el-form-item prop="role">
           <el-select v-model="ruleForm.role" placeholder="请选择用户角色">
              <el-option
                v-for="item in rolelist"
                :key="item._id"
                :label="item.name"
                :value="item._id">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item class="form-footer">
          <el-button type="primary" @click="submitForm()">注册</el-button>
        </el-form-item>

      </el-form>
  </div>
</template>

<script>
import {user_register,role_list} from '../utils/api'

export default {
  data(){
    var validatePhone= (rule, value, callback) => {
      if ( !/^1[3-9]\d{9}$/.test(value) ) {
        callback(new Error('请输入正确的手机号'));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if ( !/^\d{6}$/.test(value) ) {
        callback(new Error('请输入正确的密码'));
      } else {
        callback();
      }
    };
    var validateCheckPass = (rule, value, callback) => {
      if ( value !== this.ruleForm.pass ) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      }
    };
    var validateName = (rule, value, callback) => {
      if ( value.length < 2 ) {
        callback(new Error('请输入正确的姓名'));
      } else {
        callback();
      }
    };
    var validateRole = (rule, value, callback) => {
      if ( !value ) {
        callback(new Error('请选择一个角色'));
      } else {
        callback();
      }
    };
    return {
      rolelist:[],//角色列表
      ruleForm: {
        phone: '', //手机号
        pass: '', //密码
        checkPass: '', //确认密码
        name: '', //姓名
        role: '',//用户角色
      },
      rules: {
        phone: [
          //validator 验证器 , trigger 触发器
          { validator: validatePhone, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validateCheckPass, trigger: 'blur' }
        ],
        name: [
          { validator: validateName, trigger: 'blur' }
        ],
        role: [
          { validator: validateRole, trigger: 'blur' }
        ],
      }
    }
  },
  mounted(){
    role_list().then((res)=>{
      this.rolelist = res.data.list;
    })
  },
  methods:{
    submitForm(){
      //获取Form组件的实例, 调用实例的方法( validate内部会依次调用每一个 表单项的验证器 实现表单验证 )
      this.$refs.ruleForm.validate((valid)=>{
        if( valid ){
          var {checkPass,...params} = this.ruleForm;
          //发起注册请求.
          user_register(params).then((res)=>{
            if( res.data.code == 200 ){
              this.$message({ message:res.data.msg,type:'success' });
            }else{
              this.$message({ message:res.data.msg,type:'error' });
            }
          })
        }else{
          console.log('验证失败');
        }
      })
    }
  }
}
</script>

<style lang='scss'>
.register{
  height: 100%;
  background: url(../assets/bg.png) no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .title{
    font-size: 30px;
    padding-bottom: 20px;
  }
  .register-box{
    background-color: white;
    padding: 30px 50px;
    border-radius: 12px;
    min-width: 400px;
    .form-footer{
      display: flex;
      justify-content: center;
     .el-button--primary{
      width: 160px;
      height: 40px;
      border-radius: 20px;
     }
    }
  }
}
</style>